/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package progin;

import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Jiwandono
 */
public class chatData {
    public ArrayList<chatMessage> message;
    public ArrayList<chatMessage> roommessage;
    public ArrayList<chatMessage> servermessage;
    public ArrayList<chatRoom> room;
    public ArrayList<chatPresence> presence;
    public ArrayList<chatBanList> banlist;

    public chatData() {
        message = new ArrayList<chatMessage>();
        roommessage = new ArrayList<chatMessage>();
        servermessage = new ArrayList<chatMessage>();
        room = new ArrayList<chatRoom>();
        presence = new ArrayList<chatPresence>();
        banlist = new ArrayList<chatBanList>();
    }

    public void addPresence(String _room, String _username, String _type) {
        int n = 0;
        boolean found = false;

        while(!found && n < presence.size()) {
            if(presence.get(n).username.equals(_username) && presence.get(n).room.equals(_room)) {
                found = true;
            } else {
                n++;
            }
        }

        if(!found) {
            presence.add(new chatPresence(_username, _room, _type));
        } else {
            presence.get(n).update();
        }
    }

    public String getOwner(String _roomname) {
        int n = 0;
        boolean found = false;

        while(!found && n < room.size()) {
            if(room.get(n).name.equals(_roomname)) {
                found = true;
            } else {
                n++;
            }
        }

        if(found) {
            return room.get(n).owner;
        } else {
            return null;
        }
    }

    public void addMessage(String _sender, String _receiver, String _room, String _message) {
        message.add(new chatMessage(_sender, _receiver, _room, _message));
    }

    public void addRoomMessage(String _sender, String _room, String _message) {
        roommessage.add(new chatMessage(_sender, "", _room, _message));
    }

    public void addServerMessage(String _sender, String _receiver, String _room, String _message) {
        servermessage.add(new chatMessage(_sender, _receiver, _room, _message));
    }

    public ArrayList<chatMessage> getMessage(String _receiver, String _room) {
        ArrayList<chatMessage> messages = new ArrayList<chatMessage>();

        int n = 0;

        while(n < message.size()) {
            if(message.get(n).receiver.equals(_receiver) && message.get(n).room.equals(_room)) {
                messages.add(message.get(n));
                message.remove(n);
            } else {
                n++;
            }
        }

        return messages;
    }

    public void addBan(String _room, String _username) {
        int n = 0;
        boolean found = false;

        while(!found && n < banlist.size()) {
            if(banlist.get(n).room.equals(_room) && banlist.get(n).username.equals(_username)) {
                found = true;
            } else {
                n++;
            }
        }

        if(!found) {
            banlist.add(new chatBanList(_room, _username));
        }
    }

    public boolean isBanned(String _room, String _username) {
        int n = 0;
        boolean found = false;

        while(!found && n < banlist.size()) {
            if(banlist.get(n).room.equals(_room) && banlist.get(n).username.equals(_username)) {
                found = true;
            } else {
                n++;
            }
        }

        return found;
    }

    public void addRoom(int _parent, chatRoom.parentType _parenttype, String _name, String _owner) {
        int n = 0;
        boolean found = false;

        while(!found && n < room.size()) {
            if(room.get(n).name.equals(_name)) {
                found = true;
            } else {
                n++;
            }
        }

        if(!found) {
            room.add(new chatRoom(_parent, _parenttype, _name, _owner));
        } else {
            updateRoom(_parent, _parenttype, _name, _owner);
        }
    }

    public void joinRoom(String _roomname, String _username, String _type) {
        /*
        int n = 0;
        boolean found = false;

        while(!found && n < presence.size()) {
            if(presence.get(n).username.equals(_username) && presence.get(n).room.equals(_roomname)) {
                found = true;
            } else {
                n++;
            }
        }

        if(!found) {
            presence.add(new chatPresence(_username, _roomname));
        } else {
            presence.get(n).timestamp = new Date();
        }
         */
        addPresence(_roomname, _username, _type);
    }

    public void updateRoom(int _parent, chatRoom.parentType _parenttype, String _name, String _owner) {
        int n = 0;
        boolean found = false;

        while(!found && n < room.size()) {
            if(room.get(n).name.equals(_name)) {
                found = true;
            } else {
                n++;
            }
        }

        if(found) {
            room.set(n, new chatRoom(_parent, _parenttype, _name, _owner));
        }
    }
}
